package cc.tuhaolicai.mt.user.mobile.plugin;

import cc.tuhaolicai.mt.user.mobile.tool.ToolRedis;
import com.jfinal.plugin.IPlugin;
import org.asyou.redis.base.RedisConfig;
import org.asyou.redis.base.RedisManager;

/**
 * @author SeanDragon
 *         Create By 2017-06-23 18:58
 */
public class RedisPlugin implements IPlugin {

    private RedisConfig[] redisConfigs;

    public RedisPlugin(RedisConfig... redisConfigs) {
        this.redisConfigs = redisConfigs;
    }

    @Override
    public boolean start() {
        try {
            if (RedisManager.putRedisConfig(redisConfigs)) {
                ToolRedis.Factory.setDefaultAdapterId(redisConfigs[0].getId());
                return true;
            }
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException("初始化Redis插件失败!");
        }
        return false;
    }

    @Override
    public boolean stop() {
        try {
            for (RedisConfig redisConfig : redisConfigs) {
                RedisManager.getRedisHost(redisConfig.getId()).getRedis().close();
            }
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
        return true;
    }
}
